//
//  SocketIOTool.m
//  Socket
//
//  Created by 奇艺果 on 2017/12/21.
//  Copyright © 2017年 奇艺果. All rights reserved.
//

#import "SocketIOTool.h"
#import "QYGRoomChangedModel.h"

@import SocketIO;

@interface SocketIOTool()

@property (nonatomic, strong) SocketManager* manager;
@property (nonatomic, strong) SocketIOClient* socket;
@end

@implementation SocketIOTool

- (void)socketDisConnect{
    [self.socket disconnect];
    self.socket = nil;
    self.manager = nil;
}
- (void)socketConnectWithChannel:(NSArray*)channels completed:(void (^)(BOOL isSuccess))completed{
    
    __weak typeof(self) weakSelf = self;
    [self.socket on:@"connect" callback:^(NSArray * data, SocketAckEmitter * ack) {
        
        
        NSLog(@"--connect--------%@",[NSThread currentThread]);
        if(channels.count){
            for (NSString *channel in channels) {
                NSDictionary *dict =@{@"channel" : channel,
                                      @"auth" :  @{@"headers" : @{@"Authorization" :[NSString stringWithFormat:@"Bearer %@",[QYGKeychain token]]}}
                                      };
                [weakSelf.socket emit:@"subscribe" with:@[dict]];
            }
        }
        
        if(data){
            completed(YES);
        }
        
    }];
    
    // 设备状态改变
    [self.socket on:@"machine.status.changed" callback:^(NSArray * data, SocketAckEmitter * ack) {

        NSLog(@"--achine.status.change--------%@",[NSThread currentThread]);
        QYGDeviceStateChangedModel *deviceChangeModel = [QYGDeviceStateChangedModel mj_objectWithKeyValues:data[1]];
        if(weakSelf.machineStatusChangedBlock){
            weakSelf.machineStatusChangedBlock(deviceChangeModel);
        }


    }];
    
    [self.socket on:@"hello.world" callback:^(NSArray * data, SocketAckEmitter * ack) {
        
        NSLog(@"---hello.world-----%@",data[1]);
        
        if(weakSelf.helloBlock){
            weakSelf.helloBlock(data);
        }
    }];
    [self.socket on:@"player.queue.changed" callback:^(NSArray * data, SocketAckEmitter * ack) {

        NSLog(@"--player.queue.changed--------%@",[NSThread currentThread]);
        QYGPlayerQueueModel *playerQueueModel = [QYGPlayerQueueModel mj_objectWithKeyValues:data[1]];
        if(weakSelf.playerQueueBlock){
            weakSelf.playerQueueBlock(playerQueueModel);
        }
    }];
    [self.socket connect];
}

- (void)machineChanged{
    return;
    WS(weakSelf);
    [self.socket on:@"machine.status.changed" callback:^(NSArray * data, SocketAckEmitter * ack) {
        
        NSLog(@"--achine.status.change--------%@",[NSThread currentThread]);
        QYGDeviceStateChangedModel *deviceChangeModel = [QYGDeviceStateChangedModel mj_objectWithKeyValues:data[1]];
        if(weakSelf.machineStatusChangedBlock){
            weakSelf.machineStatusChangedBlock(deviceChangeModel);
        }
        
        
    }];
}

- (void)spectatorsChangedComplete:(void (^)(QYGRoomChangedModel *roomChangeModel))complete{
    [self.socket on:@"spectators.changed" callback:^(NSArray * data, SocketAckEmitter * ack) {
        
        NSLog(@"--achine.status.change--------%@",[NSThread currentThread]);
        QYGRoomChangedModel *roomChangeModel = [QYGRoomChangedModel mj_objectWithKeyValues:data[1]];
        if(complete){
            complete(roomChangeModel);
        }
    }];
}
- (void)gameNextOnWithChannel:(NSString *)channel Complete:(void (^)(NSString * userid))complete{
    
    NSDictionary *dict =@{@"channel" : channel,
                          @"auth" :  @{@"headers" : @{@"Authorization" :[NSString stringWithFormat:@"Bearer %@",[QYGKeychain token]]}}
                          };
    [self.socket emit:@"subscribe" with:@[dict]];
    [self.socket on:@"game.next.turn" callback:^(NSArray * data, SocketAckEmitter * ack) {
        
        NSLog(@"--game.next.turn--------%@",[NSThread currentThread]);
        id contentData = data[1];
        
        if(complete){
            complete(contentData[@"user_id"]);
        }
    }];
}
- (void)gameNextOffWithChannel:(NSString *)channel{
    
    [self.socket off:@"game.next.turn"];
    NSDictionary *dict =@{@"channel" : channel,
                          @"auth" :  @{@"headers" : @{@"Authorization" :[NSString stringWithFormat:@"Bearer %@",[QYGKeychain token]]}}
                          };
    [self.socket emit:@"unsubscribe" with:@[dict]];
  
}
- (SocketIOClient *)socket{
    if(!_socket){
        _socket = self.manager.defaultSocket;
        [_socket connect];;
    }
    return _socket;
}
- (SocketManager *)manager{
    if(!_manager){
        
        NSURL* url = [[NSURL alloc] initWithString:self.url];//@"ws://test-wwjmall.artqiyi.com:6001"
//        SocketManager* manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];
        _manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];
    }
    return _manager;
}
@end
